/*
Continuous Testing Toolkit
Copyright (C) 2009 Paul R. Holser, Jr.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package org.infinitest.toolkit;

import java.util.Arrays;

abstract class ArrayUtils {
    static boolean arraysAreEqual(Object first, Object second) {
        if (first == null)
            return second == null;
        if (second == null)
            return false;

        Class<?> firstClass = first.getClass();
        Class<?> secondClass = second.getClass();
        if (!(firstClass.isArray() && secondClass.isArray()))
            return false;
        if (!firstClass.getComponentType().equals(secondClass.getComponentType()))
            return false;

        if (boolean.class.equals(firstClass.getComponentType()))
            return Arrays.equals((boolean[]) first, (boolean[]) second);
        if (byte.class.equals(firstClass.getComponentType()))
            return Arrays.equals((byte[]) first, (byte[]) second);
        if (char.class.equals(firstClass.getComponentType()))
            return Arrays.equals((char[]) first, (char[]) second);
        if (double.class.equals(firstClass.getComponentType()))
            return Arrays.equals((double[]) first, (double[]) second);
        if (float.class.equals(firstClass.getComponentType()))
            return Arrays.equals((float[]) first, (float[]) second);
        if (int.class.equals(firstClass.getComponentType()))
            return Arrays.equals((int[]) first, (int[]) second);
        if (long.class.equals(firstClass.getComponentType()))
            return Arrays.equals((long[]) first, (long[]) second);
        if (short.class.equals(firstClass.getComponentType()))
            return Arrays.equals((short[]) first, (short[]) second);

        return Arrays.equals((Object[]) first, (Object[]) second);
    }
}
